package demo.practice.binary_search;

public class P35 {

    public static void main(String[] args) {
        P35 p35= new P35();
        p35.searchInsert(new int[]{1,3,5,6},4);
    }

    public int searchInsert(int[] nums, int target) {

        if (nums == null || nums.length == 0)
            return 0;

        int min = 0;
        int max = nums.length - 1;

        while (min < max) {
            int mid = min + (max - min) / 2;

            if (nums[mid] == target)
                return mid;

            if (nums[mid] > target) {//target在前面
                max = mid - 1;
            } else {//target在后面
                min = mid + 1;
            }
        }

        if(target>nums[min])
            return min+1;
        else
            return min;


    }


//    private int find(int[] nums, int target,int min,int max){
//            int
//    }
}
